﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Models;

namespace 包覆门.AccountManager
{
    public partial class AccountEditing : Form
    {
        public AccountEditing()
        {
            InitializeComponent();
        }
        public string EditingID { get; set; }
        public bool IsNew { get; set; }
        private AccountDAL dal = new AccountDAL();
        //保存
        private void button1_Click(object sender, EventArgs e)
        {
            if (ValidateData()) {
                Account ac = new Account();
                ac.Acbank = this.tbAcbank.Text;
                ac.AccountNum = this.tbAccount.Text;
                ac.Acname = this.tbAcname.Text;
                foreach (RadioButton rb in gbCompany.Controls) {
                    if (rb.Checked) {
                        ac.Company = Convert.ToInt32(rb.Tag);
                    }
                }
                if (IsNew)
                {
                    if (dal.SaveData(ac) == 1)
                    {
                        MessageBox.Show("保存成功");
                        DialogResult = DialogResult.OK;
                    }
                    else {
                        DialogResult = DialogResult.No;
                    }
                }
                else 
                {
                    ac.Aid = Convert.ToInt32(EditingID);
                    if (dal.UpdateData(ac) == 1)
                    {
                        MessageBox.Show("保存成功");
                        DialogResult = DialogResult.OK;
                    }
                    else {
                        MessageBox.Show("保存失败");
                        DialogResult = DialogResult.No;
                    }
                
                }
            }
        }
        //取消
        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
            Close();
        }

        private void AccountEditing_Load(object sender, EventArgs e)
        {
            if (IsNew) { } else {
                Account ac = dal.GetById(EditingID);
                this.tbAccount.Text = ac.AccountNum;
                this.tbAcname.Text = ac.Acname;
                this.tbAcbank.Text = ac.Acbank;
                foreach (RadioButton rb in this.gbCompany.Controls) {
                    if (ac.Company.ToString().Equals(rb.Tag.ToString())) {
                        rb.Checked = true;
                    }
                }
            }
        }
        //验证是否填写
        private bool ValidateData() {
            string acname = this.tbAcname.Text;
            string acbank =this.tbAcbank.Text;
            string account =this.tbAccount.Text;
            bool flag = true;
            if (string.IsNullOrWhiteSpace(acname))
            {
                MessageBox.Show("户主名不能为空");
                return false;
            }
            if (string.IsNullOrWhiteSpace(acbank))
            {
                MessageBox.Show("开户行不能为空");
                return false;
            }
            if (string.IsNullOrWhiteSpace(account))
            {
                MessageBox.Show("账户不能为空");
                return false;
            }

            return flag;
        }
    }
}
